package com.spd.shop.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.spd.shop.service.IRenterService;
import com.spd.shop.entity.Renter;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 孙沛栋
 * @since 2023-03-17
 */
@RestController
@RequestMapping("/renter")
public class RenterController {

    @Resource
    private IRenterService renterService;

    //新增或者更新
    @PostMapping
    public boolean save(@RequestBody Renter renter) {
        return renterService.saveOrUpdate(renter);
    }

    //删除
    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return renterService.removeById(id);
    }

    //批量删除
    @PostMapping("/del/batch")
    public boolean deleteBatch(@RequestBody List<Integer> ids) {
        return renterService.removeBatchByIds(ids);
    }

    //查询所有数据
    @GetMapping
    public List<Renter> findAll() {
        return renterService.list();
    }

    //根据id查询
    @GetMapping("/{id}")
    public Renter findOne(@PathVariable Integer id) {
        return renterService.getById(id);
    }

    //分页查询
    @GetMapping("/page")
    public Page<Renter> findPage(@RequestParam Integer pageNum,
                                 @RequestParam Integer pageSize,
                                 @RequestParam(defaultValue = "") String renterName,
                                 @RequestParam(defaultValue = "") String area,
                                 @RequestParam(defaultValue = "") String phone,
                                 @RequestParam(defaultValue = "") String createTime,
                                 @RequestParam(defaultValue = "") String rent) {
        QueryWrapper<Renter> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        if (!"".equals(renterName)) {
            queryWrapper.like("renter_name", renterName);
        }
        if (!"".equals(area)) {
            queryWrapper.like("area", area);
        }
        if (!"".equals(phone)) {
            queryWrapper.like("phone", phone);

        }
        if (!"".equals(rent)) {
            queryWrapper.like("rent", rent);

        }
        if (!"".equals(createTime)) {
            queryWrapper.like("create_time", createTime);

        }
        return renterService.page(new Page<>(pageNum, pageSize), queryWrapper);
    }


}

